package chat.server;

import pojo.User;
import utils.ServerConfig;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

//用户端发送聊天信息
public class ServerChat {
    private User user;

    public ServerChat(User user) {
        this.user = user;
    }

    public void run(String a) {
        Socket socket = null;
        OutputStream outputStream = null;
        try {
            //连接服务器
            socket = new Socket(InetAddress.getByName(ServerConfig.serverConfigString("InetAddress")), ServerConfig.serverConfig("ServerPort"));
            outputStream = socket.getOutputStream();
            //发送信息
            outputStream.write(a.getBytes(StandardCharsets.UTF_8));

//                outputStream.close();
//                socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
